home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / MCASM.RAR / MC_ASM.EXE / WROX_ASM / CH10 / PROGRAMS / ARCH.C next >
C/C++ Source or Header  |  1994-05-28  |  9KB  |  410 lines

  1. /* Demo compression program */
  2. /* Malakhov K.A. 1994 */
  3. /* The compressed data is writes by blocks with size BUFSIZE.*/
  4. /* The structure of block:
  5.     Fields        Meaning
  6.      WORD      length of code field
  7.      BYTE      'C' or 'U' - compressed or uncompressed code
  8.      BYTES        code
  9. */
  10.  
  11. #include <stdio.h>
  12. #include <dos.h>
  13.  
  14. #define BUFSIZE 30000
  15.  
  16. extern int rle_encode(char*,int,char*);
  17. extern int rle_decode(char*,int,char*);
  18. extern int lzw_encode(char*,int,char*);
  19. extern int lzw_decode(char*,int,char*);
  20. extern int haff_encode(char*,int,char*,int);
  21. extern int haff_decode(char*,int,char*,int);
  22. extern int arif_encode(char*,int,char*,int);
  23. extern int arif_decode(char*,int,char*,int);
  24.  
  25. void *malloc();
  26.  
  27. void main(int argc, char *argv[])
  28. {
  29.    FILE *input_file, *output_file;
  30.    char input_file_name[81],output_file_name[81];
  31.    char *buffi,*buffo;
  32.    int *bfi,*bfo;
  33.    int fsize,fsize1;
  34.    char ch;
  35.  
  36. // Allocate memory
  37.    buffi=malloc(BUFSIZE+3);
  38.    buffo=malloc(BUFSIZE+3);
  39.  
  40.    bfi=(int *) buffi;
  41.    bfo=(int *) buffo;
  42. // Check allocating results
  43.    if (buffi==NULL) {
  44.       printf("Error allocating buffer's space!\n");
  45.       exit(1);
  46.    }
  47.    if (buffo==NULL) {
  48.       printf("Error allocating buffer's space!\n");
  49.       free(buffi);
  50.       exit(1);
  51.    }
  52. // Check parameter string
  53.    if ((argc!=5) || (strpbrk(argv[1],"RrZzHhAa")==NULL) ||\
  54.             (strpbrk(argv[2],"EeDd")==NULL))
  55.    {
  56.     printf("arch.exe method option infile outfile\n");
  57.     printf(" method:\n");
  58.     printf("        r - RLE\n");
  59.     printf("        z - LZW\n");
  60.     printf("        h - Haffman\n");
  61.     printf("        a - Arifmetic\n");
  62.     printf(" option:\n");
  63.     printf("        e - encode\n");
  64.     printf("        d - decode\n");
  65.     free(buffi);
  66.     free(buffo);
  67.     exit(0);
  68.    }
  69.    else {
  70.     strcpy(input_file_name,argv[3]);
  71.     strcpy(output_file_name,argv[4]);
  72.    }
  73. // Open files
  74.    input_file=fopen(input_file_name,"rb");
  75.    output_file=fopen(output_file_name,"wb");
  76.    if (input_file == NULL || output_file == NULL) {
  77.       printf("Error opening files\n");
  78.       free(buffi);
  79.       free(buffo);
  80.       exit(1);
  81.    }
  82.    printf("Wait...\n");
  83. // Check methods
  84. // RLE RLE RLE RLE RLE RLE RLE RLE RLE RLE RLE RLE RLE RLE RLE RLE RLE RLE RLE
  85.    if (strpbrk(argv[1],"Rr")!=NULL)  // RLE
  86.    {
  87.     if (strpbrk(argv[2],"Ee")!=NULL) // Encode
  88.     {
  89. // Main loop
  90.         while
  91.         ((fsize=fread(buffi+3,sizeof(char),BUFSIZE,input_file))!=0)
  92.         {
  93.             bfi[0]=fsize; // Store the size of data
  94.             if ((fsize1=rle_encode(buffi+3,fsize,buffo+3))!=0)
  95.             {
  96.             // Compressed
  97.                 bfo[0]=fsize1;
  98.                 buffo[2]='C';
  99.                 fwrite(buffo,sizeof(char),fsize1+3,\
  100.                     output_file);
  101.             }
  102.             else {
  103.             // Uncompressed
  104.                 buffi[2]='U';
  105.                 fwrite(buffi,sizeof(char),fsize+3,\
  106.                     output_file);
  107.  
  108.             }
  109.         }
  110.     }
  111.     else  //Decode
  112.     {
  113.     // Main loop
  114.         while ((fsize=fread(buffi,sizeof(char),3,input_file))==3)
  115.         {
  116.         // Read block
  117.             fsize=bfi[0];
  118.             ch=buffi[2];
  119.             fsize1=fread(buffi,sizeof(char),fsize,input_file);
  120.             // Check data structure
  121.             if (fsize==fsize1)
  122.             {
  123.                if (ch=='C')
  124.                {
  125.                // Compressed
  126.                 fsize=rle_decode(buffi,fsize1,buffo);
  127.                 if (fsize==0)
  128.                 {
  129.                     printf("Internal RLE error\n");
  130.                     free(buffi);
  131.                     free(buffo);
  132.                     fclose(input_file);
  133.                     fclose(output_file);
  134.                     exit(1);
  135.                 }
  136.                 fwrite(buffo,sizeof(char),fsize,output_file);
  137.                }
  138.                else
  139.                // Uncompressed
  140.                 fwrite(buffi,sizeof(char),fsize,output_file);
  141.             }
  142.             else {
  143.             // File structure error
  144.                 printf("Error in compressed file\n");
  145.                 free(buffi);
  146.                 free(buffo);
  147.                 fclose(input_file);
  148.                 fclose(output_file);
  149.                 exit(1);
  150.             }
  151.         }
  152.         if (fsize!=0) {
  153.         // File structure error
  154.             printf("Error in compressed file\n");
  155.             free(buffi);
  156.             free(buffo);
  157.             fclose(input_file);
  158.             fclose(output_file);
  159.             exit(1);
  160.         }
  161.     }
  162.    }
  163. //---------------------------------------------------------------------------
  164.  
  165.    else if (strpbrk(argv[1],"Zz")!=NULL) //LZW
  166.    {
  167.     if (strpbrk(argv[2],"Ee")!=NULL) // Encode
  168.     {
  169. // Main loop
  170.         while
  171.         ((fsize=fread(buffi+3,sizeof(char),BUFSIZE,input_file))!=0)
  172.         {
  173.             bfi[0]=fsize; // Store the size of data
  174.             if ((fsize1=lzw_encode(buffi+3,fsize,buffo+3))!=0)
  175.             {
  176.             // Compressed
  177.                 bfo[0]=fsize1;
  178.                 buffo[2]='C';
  179.                 fwrite(buffo,sizeof(char),fsize1+3,\
  180.                     output_file);
  181.             }
  182.             else {
  183.             // Uncompressed
  184.                 buffi[2]='U';
  185.                 fwrite(buffi,sizeof(char),fsize+3,\
  186.                     output_file);
  187.  
  188.             }
  189.         }
  190.     }
  191.     else  //Decode
  192.     {
  193.     // Main loop
  194.         while ((fsize=fread(buffi,sizeof(char),3,input_file))==3)
  195.         {
  196.         // Read block
  197.             fsize=bfi[0];
  198.             ch=buffi[2];
  199.             fsize1=fread(buffi,sizeof(char),fsize,input_file);
  200.             // Check data structure
  201.             if (fsize==fsize1)
  202.             {
  203.                if (ch=='C')
  204.                {
  205.                // Compressed
  206.                 fsize=lzw_decode(buffi,fsize1,buffo);
  207.                 if (fsize==0)
  208.                 {
  209.                     printf("Internal RLE error\n");
  210.                     free(buffi);
  211.                     free(buffo);
  212.                     fclose(input_file);
  213.                     fclose(output_file);
  214.                     exit(1);
  215.                 }
  216.                 fwrite(buffo,sizeof(char),fsize,output_file);
  217.                }
  218.                else
  219.                // Uncompressed
  220.                 fwrite(buffi,sizeof(char),fsize,output_file);
  221.             }
  222.             else {
  223.             // File structure error
  224.                 printf("Error in compressed file\n");
  225.                 free(buffi);
  226.                 free(buffo);
  227.                 fclose(input_file);
  228.                 fclose(output_file);
  229.                 exit(1);
  230.             }
  231.         }
  232.         if (fsize!=0) {
  233.         // File structure error
  234.             printf("Error in compressed file\n");
  235.             free(buffi);
  236.             free(buffo);
  237.             fclose(input_file);
  238.             fclose(output_file);
  239.             exit(1);
  240.         }
  241.     }
  242.    }
  243.  
  244. // HAFF HAFF HAFF HAFF HAFF HAFF HAFF HAFF HAFF HAFF HAFF HAFF HAFF HAFF HAFF
  245.    else if (strpbrk(argv[1],"Hh")!=NULL)  // Haffman
  246.    {
  247.     if (strpbrk(argv[2],"Ee")!=NULL) // Encode
  248.     {
  249. // Main loop
  250.         while
  251.         ((fsize=fread(buffi+3,sizeof(char),BUFSIZE,input_file))!=0)
  252.         {
  253.             bfi[0]=fsize;
  254.             if ((fsize1=haff_encode(buffi+3,fsize,buffo+3,\
  255.                         BUFSIZE))!=0)
  256.             {
  257.             // Compressed
  258.                 bfo[0]=fsize1;
  259.                 buffo[2]='C';
  260.                 fwrite(buffo,sizeof(char),fsize1+3,\
  261.                 output_file);
  262.             }
  263.             else {
  264.             // Uncompressed
  265.                 buffi[2]='U';
  266.                 fwrite(buffi,sizeof(char),fsize+3,\
  267.                 output_file);
  268.             }
  269.         }
  270.     }
  271.     else  //Decode
  272.     {
  273.     // Main loop
  274.         while ((fsize=fread(buffi,sizeof(char),3,input_file))==3)
  275.         {
  276.         // Read block
  277.             fsize=bfi[0];
  278.             ch=buffi[2];
  279.             fsize1=fread(buffi,sizeof(char),fsize,input_file);
  280.             // Check data structure
  281.             if (fsize==fsize1)
  282.             {
  283.                if (ch=='C')
  284.                {
  285.                // Compressed
  286.                 fsize=haff_decode(buffi,fsize1,buffo,\
  287.                 BUFSIZE+3);
  288.                 if (fsize==0)
  289.                 {
  290.                     printf("Internal Haffman error\n");
  291.                     free(buffi);
  292.                     free(buffo);
  293.                     fclose(input_file);
  294.                     fclose(output_file);
  295.                     exit(1);
  296.                 }
  297.                 fwrite(buffo,sizeof(char),fsize,output_file);
  298.                }
  299.                else
  300.                // Uncompressed
  301.                 fwrite(buffi,sizeof(char),fsize,output_file);
  302.             }
  303.             else {
  304.             // File structure error
  305.                 printf("Error in compressed file\n");
  306.                 free(buffi);
  307.                 free(buffo);
  308.                 fclose(input_file);
  309.                 fclose(output_file);
  310.                 exit(1);
  311.             }
  312.         }
  313.         if (fsize!=0) {
  314.         // File structure error
  315.             printf("Error in compressed file\n");
  316.             free(buffi);
  317.             free(buffo);
  318.             fclose(input_file);
  319.             fclose(output_file);
  320.             exit(1);
  321.         }
  322.     }
  323.    }
  324. //---------------------------------------------------------------------------
  325.    else if (strpbrk(argv[1],"Aa")!=NULL)  // Arifmetic
  326.    {
  327.     if (strpbrk(argv[2],"Ee")!=NULL) // Encode
  328.     {
  329. // Main loop
  330.         while
  331.         ((fsize=fread(buffi+3,sizeof(char),BUFSIZE,input_file))!=0)
  332.         {
  333.             bfi[0]=fsize; // Store the size of data
  334.             if ((fsize1=arif_encode(buffi+3,fsize,buffo+3,\
  335.                         BUFSIZE))!=0)
  336.             {
  337.             // Compressed
  338.                 bfo[0]=fsize1;
  339.                 buffo[2]='C';
  340.                 fwrite(buffo,sizeof(char),fsize1+3,\
  341.                     output_file);
  342.             }
  343.             else {
  344.             // Uncompressed
  345.                 buffi[2]='U';
  346.                 fwrite(buffi,sizeof(char),fsize+3,\
  347.                     output_file);
  348.  
  349.             }
  350.         }
  351.     }
  352.     else  //Decode
  353.     {
  354.     // Main loop
  355.         while ((fsize=fread(buffi,sizeof(char),3,input_file))==3)
  356.         {
  357.         // Read block
  358.             fsize=bfi[0];
  359.             ch=buffi[2];
  360.             fsize1=fread(buffi,sizeof(char),fsize,input_file);
  361.             // Check data structure
  362.             if (fsize==fsize1)
  363.             {
  364.                if (ch=='C')
  365.                {
  366.                // Compressed
  367.                 fsize=arif_decode(buffi,fsize1,buffo,BUFSIZE);
  368.                 if (fsize==0)
  369.                 {
  370.                     printf("Internal Arifmetic error\n");
  371.                     free(buffi);
  372.                     free(buffo);
  373.                     fclose(input_file);
  374.                     fclose(output_file);
  375.                     exit(1);
  376.                 }
  377.                 fwrite(buffo,sizeof(char),fsize,output_file);
  378.                }
  379.                else
  380.                // Uncompressed
  381.                 fwrite(buffi,sizeof(char),fsize,output_file);
  382.             }
  383.             else {
  384.             // File structure error
  385.                 printf("Error in compressed file\n");
  386.                 free(buffi);
  387.                 free(buffo);
  388.                 fclose(input_file);
  389.                 fclose(output_file);
  390.                 exit(1);
  391.             }
  392.         }
  393.         if (fsize!=0) {
  394.         // File structure error
  395.             printf("Error in compressed file\n");
  396.             free(buffi);
  397.             free(buffo);
  398.             fclose(input_file);
  399.             fclose(output_file);
  400.             exit(1);
  401.         }
  402.  
  403.     }
  404.    }
  405.    fclose(input_file);
  406.    fclose(output_file);
  407.    free(buffi);
  408.    free(buffo);
  409. }
  410.